package com.example.cd5160866.urlapp;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class Demo6Activity extends AppCompatActivity {
    EditText edt;

    ViewGroup container;


    Button submitView;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.list_vertical:
                edt.setText("187 1167 1310");
                break;
            case R.id.list_horizontal:
                break;
            case R.id.gridview:
                break;
            case R.id.add:
                break;
            case R.id.delete:
                break;
            case R.id.add_diver:
                break;
            case R.id.remove_diver:
                break;
            case R.id.add_header:
                break;
            case R.id.add_footer:
                break;
        }
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_text_view);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        container = (ViewGroup) findViewById(R.id.container);
        submitView = (Button) findViewById(R.id.btn_submit);
        edt = (EditText) findViewById(R.id.edt);
        alwaysShowViewInInputModel(submitView, false);
    }

    /**
     * 在输入模式下可让某个view始终处于可见的场景 （常用于输入框确定按钮被键盘遮挡的情况）
     *
     * @param targetView    可能被遮挡的view
     * @param scrollContent 是移动整个布局界面还是仅仅移动传入的那个view
     */
    public static void alwaysShowViewInInputModel(final View targetView, final boolean scrollContent) {
        final View rootView = targetView.getRootView();
        if (null == rootView) {
            return;
        }
        rootView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                Rect rect = new Rect();
                rootView.getWindowVisibleDisplayFrame(rect);
                int[] location = new int[2];
                targetView.getLocationInWindow(location);

                View translationView = scrollContent ? rootView : targetView;
                if (rect.bottom <= (location[1] + targetView.getHeight())) {
                    int scrollHeight = (location[1] + targetView.getHeight()) - rect.bottom;
                    translationView.setTranslationY(-scrollHeight);
                } else {
                    translationView.setTranslationY(0);
                }
            }
        });
    }

}
